<template>
  <div class="album-container">
    <!-- 专辑信息区域 -->
    <div class="album-info">
      <div class="album-left">
        <image-preview :src="this.$route.query.imgUrl" class="album-cover" />
      </div>

      <div class="album-right">
        <h1 class="album-title">{{ albumInfo.albumName }}</h1>
        <div class="album-artist">公司：{{ albumInfo.company }}</div>
        <div class="album-artist">歌手：{{ albumInfo.singer }}</div>
        <div class="album-artist">简介：{{ albumInfo.description }}</div>
        <div class="album-artist">发行时间：{{ albumInfo.releaseDate }}</div>
        <div class="album-actions">
          <el-button type="primary" icon="el-icon-video-play" @click="playAll"
            >播放全部</el-button
          >
          <!-- <el-button icon="el-icon-folder-add">收藏</el-button> -->
        </div>
      </div>
    </div>

    <!-- 自定义歌曲列表 -->
    <div class="song-list">
      <div class="song-list-header">
        <span>歌曲</span>
        <span>歌手</span>
        <span>图片</span>
        <span>发行时间</span>
        <span>操作</span>
      </div>
      <div v-for="song in songList" :key="song.songId" class="song-list-item">
        <span>{{ song.songName }}</span>
        <span>{{ song.singer }}</span>
        <span>
          <image-preview :src="song.imgUrl" :width="80" :height="80" />
        </span>
        <span>{{ song.releaseDate }}</span>
        <span>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-video-play"
            @click="handlePlay(song)"
            >播放</el-button
          >
          <el-button size="mini" type="text" icon="el-icon-download">
            <a :href="song.fileUrl" download>下载</a>
          </el-button>
        </span>
      </div>
    </div>
  </div>
</template>

<script>
import { getAlbum } from "@/api/album/album";
import { getSongListByAlbumId } from "@/api/song/song";
import { addHistory } from "@/api/history/history";
export default {
  data() {
    return {
      uploadUrl: process.env.VUE_APP_BASE_API + "/api/music/upload",
      albumInfo: {
        albumName: "",
        company: "",
        singer: "",
        description: "",
        releaseDate: "",
      },
      songList: [],
    };
  },
  methods: {
    // 播放全部
    playAll() {
      //提取songList中的songId
      const songIds = this.songList.map((item) => item.songId);
      const currentSongIndex = 0;
      this.$router.push({
        path: "/player",
        query: {
          currentSongIndex: currentSongIndex,
          songIds: songIds,
        },
      });
    },
    handlePlay(row) {
      const data = {
        songId: row.songId,
        userId: localStorage.getItem("userId"),
      };
      //记录用户播放次数
      addHistory(data).then((res) => {
        // console.log("播放次数", res);
      });
      const songIds = this.songList.map((item) => item.songId);
      const currentSongIndex = songIds.indexOf(row.songId);
      console.log(currentSongIndex);

      this.$router.push({
        path: "/player",
        query: { currentSongIndex: currentSongIndex, songIds: songIds },
      });
    },

    // 获取专辑信息
    getAlbumInfo(albumId) {
      getAlbum(albumId).then((res) => {
        // console.log(res);
        this.albumInfo = res.data;
      });
    },
    // 获取歌曲列表
    getSongListByAlbumId(albumId) {
      getSongListByAlbumId(albumId).then((res) => {
        // console.log(res);
        this.songList = res;
      });
    },
  },
  created() {
    this.getAlbumInfo(this.$route.query.albumId);
    this.getSongListByAlbumId(this.$route.query.albumId);
    localStorage.setItem("isBack", "true");
  },
};
</script>

<style scoped>
.album-container {
  padding: 20px;
}

.album-info {
  display: flex;
  margin-bottom: 40px;
  padding: 20px;
  background: #fafafa;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.album-left {
  margin-right: 30px;
}

.album-cover {
  width: 200px;
  height: 200px;
  border-radius: 8px;
}

.album-right {
  flex: 1;
}

.album-title {
  font-size: 24px;
  margin-bottom: 15px;
}

.album-artist {
  color: #666;
  margin-bottom: 10px;
}

.album-actions {
  margin-top: 20px;
}

.song-list {
  margin-top: 20px;
}

.song-list-header {
  display: flex;
  justify-content: space-between;
  padding: 10px;
  background-color: #f0f0f0;
  font-weight: bold;
}

.song-list-item {
  display: flex;
  justify-content: space-between;
  padding: 10px;
  border-bottom: 1px solid #e0e0e0;
}

.song-list-item span {
  flex: 1;
  text-align: center;
}
</style>
